package com.ten.mall.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.ItemService;
import com.ten.mall.service.PihaoService;
import com.ten.mall.vo.Item;
import com.ten.mall.vo.Pihao;

@Controller
public class PihaoController {
	@Autowired
	private PihaoService pihaoService;
	
	@Autowired
	  private ItemService itemService;
	/**
	 * 批号列表
	 * @param start
	 * @param size
	 * @param res
	 * @param name
	 * @param goodsName
	 * @return
	 */
	@RequestMapping("/admin/pihaoList")
	public String getAllPihao(@RequestParam(name="start",defaultValue="1") Integer start,
            				  @RequestParam(name="size",defaultValue="10") Integer size,
            				  HttpServletRequest res,String name,String goodsName){
		Map<String,Object> map =pihaoService.selectPihao(start, size, name, goodsName);
		List<Pihao> list =(List<Pihao>) map.get("list");
		res.setAttribute("list", list);
		Long mySize=(Long) map.get("total");
		int total =mySize.intValue()/10+1;
		res.setAttribute("page", start);
		res.setAttribute("total", total);
		return "jsp/pihaoList";
	}
	/**
	 * 添加批号
	 * @param pihao
	 * @return
	 */
	@RequestMapping("/admin/pihaoAdd")
	public String pihaoAdd(Pihao pihao){
		if(pihao.getId()==null){
			pihaoService.insertPihao(pihao);
		}else{
			pihaoService.updatePihao(pihao);
		}
		return "redirect:/admin/pihaoList";
	}
	/**
	 * 删除批号
	 * @param id
	 * @return
	 */
	@RequestMapping("/admin/pihaoDel")
	@ResponseBody
	public String pihaoDel(Integer id){
		String rs="ok";
		try {
			if(id!=null){
				pihaoService.deletePihao(id);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rs="error";
		}
		return rs;
	}
	
	@RequestMapping("/admin/toPihaoAdd")
	public String toAddPihao(Integer id,HttpServletRequest res){
		if(id!=null){
			Pihao pihao =pihaoService.selectById(id);
			res.setAttribute("p", pihao);
		}
		List<Item> list = itemService.selectAll();
		res.setAttribute("list", list);
		return "jsp/pihaoAdd";
	}
	
	@RequestMapping("/phone/report")
	public String getReport(Integer id,HttpServletRequest request){
		if(id!=null){
			List<Pihao> list =pihaoService.selectBygoodsId(id, null);
			request.setAttribute("list", list);
		}
		return "phone/report";
	}
	
	@RequestMapping("/phone/getPic")
	@ResponseBody
	public String getReportPic(Integer id){
		String result="";
		if(id!=null){
			Pihao pihao =pihaoService.selectById(id);
			result=pihao.getPiReport();
		}
		return result;
	}
}
